<?php
class GroupMembersModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("group_members", "id");
        $this->addField("id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("groupid", Model::TYPE_VARCHAR);
        $this->addField("name", Model::TYPE_VARCHAR);
        $this->addField("contactperson", Model::TYPE_VARCHAR);
        $this->addField("email", Model::TYPE_VARCHAR);
        $this->addField("wantnewsletter", Model::TYPE_INT);
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function unsubscribeEmail($email) {
        $query = "UPDATE group_members set wantnewsletter=0 where email='$email'";
        Database::executeQuery($query);
    }

    public function getHonoraryAmbassadors() {
        $query = "SELECT id AS company_id, name AS companyname, email AS email1 FROM group_members WHERE wantnewsletter>=1 AND groupid='ambassadors'";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function getEmbassies() {
        $query = "SELECT id AS company_id, ame AS companyname, email AS email1 FROM group_members WHERE wantnewsletter>=1 AND groupid='embassies'";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function getChambers() {
        $query = "SELECT id AS company_id, name AS companyname, email AS email1 FROM group_members WHERE wantnewsletter>=1 AND groupid='chambers'";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function changelink($params) {
        $query = QueryHelper::update("group_members", array(
            groupid => $params['membertype'],
            name => $params['name'],
            email => $params['email'],
            contactperson => $params['contactperson'],
        ), "id=" . $params['id']);
        Database::executeQuery($query);
    }

    public function getMemberList($groupId) {
        $query = "SELECT id, name, email, contactperson FROM group_members WHERE groupid='$groupId'";
        $res = Database::executeQuery($query);
        $data = Database::getDatafromResource($res);
        return $data;
    }

    public function saveMemberToGroup($groupName, $name, $email, $contact) {
        $query = "SELECT count(*) AS c FROM group_members WHERE groupid='$groupName'";
        $res = Database::executeQuery($query);
        $count = Database::getSimpleField($res);
        $query = "INSERT INTO group_members (groupid, name, email, contactperson) VALUES ('$groupName', '$name', '$email', '$contact')";
        Database::executeQuery($query);
    }

    public function removeMemberFromGroup($idlist) {
        $query = "DELETE FROM group_members WHERE id IN (" . implode(",", $idlist) . ")";
        Database::executeQuery($query);
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
